Skip to content

SANDBOX-1282 | refactor: replace "wrapf" calls with stdlib wrap calls#526

Merged
MikelAlejoBR merged 2 commits intocodeready-toolchain:masterfrom
MikelAlejoBR:SANDBOX-1282-refactor-replace-wrapf-errorf
Apr 23, 2026
Merged

SANDBOX-1282 | refactor: replace "wrapf" calls with stdlib wrap calls#526
MikelAlejoBR merged 2 commits intocodeready-toolchain:masterfrom
MikelAlejoBR:SANDBOX-1282-refactor-replace-wrapf-errorf

Conversation

@MikelAlejoBR
Copy link
Copy Markdown
Contributor

@MikelAlejoBR MikelAlejoBR commented Apr 22, 2026

There is no need to keep the legacy "wrapf" calls to wrap errors anymore, so the idea is to remove them in favor of the wrapping calls of the stdlib.

Related PRs

Jira ticket

[SANDBOX-1282]

Summary by CodeRabbit

  • Refactor
    • Updated internal error handling across client, cluster, and template components to use native Go error wrapping semantics.

There is no need to keep the legacy "wrapf" calls to wrap errors
anymore, so the idea is to remove them in favor of the wrapping calls of
the stdlib.

SANDBOX-1282
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 22, 2026

Walkthrough

Updates error handling across three packages to use Go's native error wrapping (fmt.Errorf with %w verb) instead of the external github.com/pkg/errors library. Preserves contextual messages while ensuring original errors remain unwrappable via standard Go error semantics.

Changes

Cohort / File(s) Summary
Error Wrapping Modernization
pkg/client/client.go, pkg/cluster/service.go, pkg/template/nstemplatetiers/nstemplatetier_generator.go
Replaced github.com/pkg/errors.Wrapf with fmt.Errorf using %w verb across error-handling sites in ApplyObject, applyObject, Apply, NewClusterConfig, loadTemplatesByTiers, ensureObject, and manifest generation paths. Maintains contextual error messages while leveraging native Go error wrapping.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately describes the main change: replacing legacy error-wrapping calls from github.com/pkg/errors with stdlib fmt.Errorf.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@metlos metlos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (3)
pkg/template/nstemplatetiers/nstemplatetier_generator.go (1)

60-186: Remaining errors.Wrap / errors.Errorf not converted.

Lines 60, 66, 72, and 186 still use github.com/pkg/errors. To fully complete the stdlib-wrapping refactor in this file (and drop the github.com/pkg/errors import), convert these as well — e.g.:

-        return errors.Wrap(err, "unable to init NSTemplateTier generator")
+        return fmt.Errorf("unable to init NSTemplateTier generator: %w", err)
...
-            return nil, errors.Errorf("unable to load templates: unknown scope for file '%s'", name)
+            return nil, fmt.Errorf("unable to load templates: unknown scope for file '%s'", name)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/template/nstemplatetiers/nstemplatetier_generator.go` around lines 60 -
186, The file still uses github.com/pkg/errors in loadTemplatesByTiers (and the
surrounding init code) — replace remaining errors.Wrap and errors.Errorf usages
with stdlib fmt.Errorf using %w for wrapping (e.g. return fmt.Errorf("unable to
init NSTemplateTier generator: %w", err) and return fmt.Errorf("unable to load
templates: unknown scope for file '%s'", name) or use %w where wrapping is
required), update the error returns in newNSTemplateTierGenerator and
loadTemplatesByTiers accordingly, and remove the pkg/errors import; search for
errors.Wrap, errors.Errorf and convert them to fmt.Errorf with proper %w
wrapping and plain formatting where no wrapping is needed.
pkg/client/client.go (1)

238-254: Double-wrapping in Apply and leftover errors.Wrap.

Two observations:

  1. Line 254 wraps the error returned by ApplyObject, but ApplyObject (line 100) already wraps with the exact same "unable to create resource of kind: %s, version: %s" message. The resulting error chain will contain that prefix twice (e.g., unable to create resource of kind: X, version: Y: unable to create resource of kind: X, version: Y: <cause>). Consider returning the error unchanged from Apply, or removing the duplicate wrap at one of the two layers. Note: this duplication predates this PR, but the conversion is a good moment to fix it.
  2. Line 238 still uses errors.Wrap from github.com/pkg/errors. For consistency with the rest of this refactor, convert it to fmt.Errorf("unable to set controller references: %w", err) so the pkg/errors import can eventually be dropped.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/client/client.go` around lines 238 - 254, The Apply method is
double-wrapping errors from ApplyObject and still uses errors.Wrap elsewhere;
fix by returning the error from Apply unchanged (i.e., replace the fmt.Errorf
wrap in Apply that references toolchainObject.GetObjectKind().GroupVersionKind()
with a plain return false, err) so ApplyObject remains the single author of that
context, and also replace any uses of errors.Wrap (the one that wraps "unable to
set controller references") with fmt.Errorf("unable to set controller
references: %w", err) so pkg/errors can be removed consistently; target the
Apply function and the code path that sets controller references (where
errors.Wrap is used) to make these changes.
pkg/cluster/service.go (1)

68-82: Remaining errors.Wrap calls not converted.

Lines 68, 82, 108, and 160 still use github.com/pkg/errors (errors.Wrap / errors.Errorf). If the goal of the PR is to standardize on stdlib wrapping, these sites in the same file should be converted too; otherwise the github.com/pkg/errors import cannot be removed and the refactor remains inconsistent.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/cluster/service.go` around lines 68 - 82, Several error uses in this file
still call github.com/pkg/errors (errors.Wrap / errors.Errorf); update those to
use the standard library error wrapping so the pkg/errors import can be removed.
Replace errors.Wrap(err, "msg") with fmt.Errorf("msg: %w", err) and replace
errors.Errorf("format", args...) with fmt.Errorf("format", args...), targeting
the error sites within functions such as addToolchainCluster (and any other
functions in this file that call errors.Wrap/errors.Errorf), and ensure the file
imports "fmt" and removes the "github.com/pkg/errors" import.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@pkg/client/client.go`:
- Around line 238-254: The Apply method is double-wrapping errors from
ApplyObject and still uses errors.Wrap elsewhere; fix by returning the error
from Apply unchanged (i.e., replace the fmt.Errorf wrap in Apply that references
toolchainObject.GetObjectKind().GroupVersionKind() with a plain return false,
err) so ApplyObject remains the single author of that context, and also replace
any uses of errors.Wrap (the one that wraps "unable to set controller
references") with fmt.Errorf("unable to set controller references: %w", err) so
pkg/errors can be removed consistently; target the Apply function and the code
path that sets controller references (where errors.Wrap is used) to make these
changes.

In `@pkg/cluster/service.go`:
- Around line 68-82: Several error uses in this file still call
github.com/pkg/errors (errors.Wrap / errors.Errorf); update those to use the
standard library error wrapping so the pkg/errors import can be removed. Replace
errors.Wrap(err, "msg") with fmt.Errorf("msg: %w", err) and replace
errors.Errorf("format", args...) with fmt.Errorf("format", args...), targeting
the error sites within functions such as addToolchainCluster (and any other
functions in this file that call errors.Wrap/errors.Errorf), and ensure the file
imports "fmt" and removes the "github.com/pkg/errors" import.

In `@pkg/template/nstemplatetiers/nstemplatetier_generator.go`:
- Around line 60-186: The file still uses github.com/pkg/errors in
loadTemplatesByTiers (and the surrounding init code) — replace remaining
errors.Wrap and errors.Errorf usages with stdlib fmt.Errorf using %w for
wrapping (e.g. return fmt.Errorf("unable to init NSTemplateTier generator: %w",
err) and return fmt.Errorf("unable to load templates: unknown scope for file
'%s'", name) or use %w where wrapping is required), update the error returns in
newNSTemplateTierGenerator and loadTemplatesByTiers accordingly, and remove the
pkg/errors import; search for errors.Wrap, errors.Errorf and convert them to
fmt.Errorf with proper %w wrapping and plain formatting where no wrapping is
needed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 28d3ec50-859f-49e4-8798-4015aa9a0b4b

📥 Commits

Reviewing files that changed from the base of the PR and between 61a6731 and 039bc41.

📒 Files selected for processing (3)
  • pkg/client/client.go
  • pkg/cluster/service.go
  • pkg/template/nstemplatetiers/nstemplatetier_generator.go

@MikelAlejoBR MikelAlejoBR merged commit 94789ab into codeready-toolchain:master Apr 23, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants